﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CheckList
{
    public partial class formWyroby : Form
    {
        public string [] currentProgram;
        


        public formWyroby()
        {
            InitializeComponent();
        }

        private void formWyroby_Load(object sender, EventArgs e)
        {
            wyrobyLoad();
        }



        private void wyrobyLoad()
        {
            DataSet wyrobyDataSet = new DataSet();
            try
            {
                // Creates and opens an ODBC connection
                string strConnString = classConfig.sqlConnString;
                OdbcConnection conn;
                conn = new OdbcConnection(strConnString.Trim());
                conn.Open();

                //Creates the data adapter
                OdbcDataAdapter wyrobyDataAdapter = new OdbcDataAdapter("SELECT * FROM program ORDER BY nazwa_pl", conn);
                //Fills dataset with the records from database
                wyrobyDataAdapter.Fill(wyrobyDataSet, "program");

                       
                //Update dataset with the records from CSV
                DataTable wyrobyDataTable = wyrobyDataSet.Tables["program"];
                //closes the connection
                conn.Close();

                this.listBoxWyroby.DataSource = wyrobyDataSet;
                this.listBoxWyroby.DisplayMember = "program.nazwa_pl";
                this.listBoxWyroby.ValueMember = "program.id_program";
            }
            catch (Exception e) //Error
            {
                MessageBox.Show(e.Message, "Error - Wczytywania listy Wyrobów", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
 
        }

        private void openFormKomplety()
        {
            formKompletMain formWyrobMain_ = new formKompletMain(currentProgram);
            // open form only if is not open already

            // iterate through all open forms
            foreach (Form frm in Application.OpenForms)
            {
                if (frm is formKompletMain)
                {
                    // open already so close and open new
                    frm.Close();
                    break;
                }
    

            }
            formWyrobMain_.MdiParent = ParentForm;
            formWyrobMain_.Show();
            //formWyrobMain_.BringToFront();
        }


        private void listBoxWyroby_Click(object sender, EventArgs e)
        {
            // MessageBox.Show(listBoxWyroby.GetItemText(listBoxWyroby.SelectedItem), "listBoxWyroby_SelectedValueChanged");
            currentProgram = new string[] { listBoxWyroby.SelectedValue.ToString(), listBoxWyroby.GetItemText(listBoxWyroby.SelectedItem) };
            openFormKomplety();
        }

        private void buttonDodajWyrob_Click(object sender, EventArgs e)
        {
            formNowyEdytujWyrob formNowyWyrob_ = new formNowyEdytujWyrob(null);
            DialogResult dialogResult = formNowyWyrob_.ShowDialog();
            wyrobyLoad();
        }

        private void buttonEdytujWyrob_Click(object sender, EventArgs e)
        {
            formNowyEdytujWyrob formNowyWyrob_ = new formNowyEdytujWyrob(listBoxWyroby.SelectedValue.ToString());
            DialogResult dialogResult = formNowyWyrob_.ShowDialog();
            wyrobyLoad();
        }

        private void buttonUsunWyrob_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr = MessageBox.Show("Czy napewno usunąc wskazany Wyrób " + listBoxWyroby.GetItemText(listBoxWyroby.SelectedItem) + " ?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                switch (dr)
                {
                    case DialogResult.Yes:
                        string query0 = "SELECT id_komplet FROM komplet WHERE id_program = " + listBoxWyroby.SelectedValue.ToString();
                        DataTable dt = classConfig.getDTable(query0, classConfig.sqlConnString);
                        foreach (DataRow row in dt.Rows)
                        {
                            string id_komplet = row[0].ToString();
                            string query1 = "DELETE FROM elementy WHERE id_komplet = " + id_komplet;
                            classConfig.exec(query1, classConfig.sqlConnString);
                            string query2 = "DELETE FROM komplet WHERE id_komplet = " + id_komplet;
                            classConfig.exec(query2, classConfig.sqlConnString);
                            string query3 = "DELETE FROM paczka WHERE id_komplet = " + id_komplet;
                            classConfig.exec(query3, classConfig.sqlConnString);
                            string query4 = "DELETE FROM paczka_kod_kres WHERE id_komplet = " + id_komplet;
                            classConfig.exec(query4, classConfig.sqlConnString);
                        }
                        string query6 = "DELETE FROM program WHERE id_program = " + listBoxWyroby.SelectedValue.ToString();
                        classConfig.exec(query6, classConfig.sqlConnString);
                        MessageBox.Show("Usunięto wyrób " + listBoxWyroby.GetItemText(listBoxWyroby.SelectedItem));
                        wyrobyLoad();
                        break;

                    case DialogResult.No:
                        MessageBox.Show("Zrezygnowano.");
                        break;
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error - Wczytywania listy Wyrobów", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }   
    }
}
